package otherPractice;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpClientUtill {


    public static void main(String[] args) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String addressUrl = "http://47.100.58.250/maze.html";
                    URL url = new URL(addressUrl);
                    //得到connection对象。
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    //设置请求方式
                    connection.setRequestMethod("GET");
                    //连接
                    connection.connect();
                    //得到响应码
                    int responseCode = connection.getResponseCode();
                    if(responseCode == HttpURLConnection.HTTP_OK){
                        //得到响应流
                        InputStream inputStream = connection.getInputStream();
                        connection.getResponseMessage();
                        System.out.println("--- connection.getResponseMessage()=   --" + connection.getResponseMessage());
                        StringBuilder sb = new StringBuilder("响应内容=");
                        byte[] b = new byte[1024];
                        int len = 0;

                        while ((len = inputStream.read(b)) != -1){
                            sb.append(new String(b, 0, len, "utf-8"));
                            //sb.append(inputStream.read());
                        }


                        //将响应流转换成字符串
                        //String result = inputStream.toString();//将流转换为字符串。
                        System.out.println("---   result = ----" + sb);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }



}
